package presentation;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import metier.AquariumTechniqueException;

import user.AquariumDTO;
import user.PoissonDTO;
import util.DateUtil;
import application.GererAquariumCtrl;
import application.GererPoissonCtrl;
import application.HelperException;

public class CreerPoisson extends JPanel {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private PanelConteneur pc;
	
	//boutons de menu a gauche
	private JPanel jpButton =new JPanel(new GridLayout(4,1));
	private JPanel jpPrincipal = new JPanel (new BorderLayout());
	private JPanel jpInfosGenerales = new JPanel (new GridLayout());
	private JPanel jpInfosGenerales1 = new JPanel (new GridLayout(10,1));
	private JPanel jpInfosGenerales2 = new JPanel (new GridLayout(10,1));
	
	private JPanel jpNom = new JPanel (new FlowLayout(FlowLayout.RIGHT));
	private JPanel jpNom2 = new JPanel (new FlowLayout(FlowLayout.LEFT));
	
	private JPanel jpBoutonsBas = new JPanel (new FlowLayout(FlowLayout.RIGHT));
	
	private JButton jbInserer ;
	private JButton jbReinitialiser ;
	private JButton jbRetour;
	
	private JButton jbCreer ;
	private JButton jbModif ;
	private JButton jbSearch;
	private JButton jbSuppr; 
	
	private JLabel jlNom;
	
	private JTextField jtNom;
	
	private Dimension dimTextField ;

	// BORDURES
	private Border border1 = BorderFactory.createLineBorder(Color.black);
	
	public CreerPoisson (PanelConteneur jpC) {

		this.pc=jpC;
	
	//Dimension des TextFields
	dimTextField = new Dimension(250,25);
	
	//Boutons de changement de panel : création, modification, recherche, suppression
	this.setLayout(new BorderLayout());
	
	Color creation = new Color (49,146,57);
	jbCreer = new JButton ();
	jbCreer.setBackground(creation);
	jbCreer.setIcon(new ImageIcon("images/creation.gif"));
	
	jbModif = new JButton ();
	jbModif.setBackground(Color.WHITE);
	jbModif.setIcon(new ImageIcon("images/modification.gif"));
	
	jbSearch = new JButton ();
	jbSearch.setBackground(Color.WHITE);
	jbSearch.setIcon(new ImageIcon("images/recherche.gif"));
	
	jbSuppr = new JButton ();
	jbSuppr.setBackground(Color.WHITE);
	jbSuppr.setIcon(new ImageIcon("images/suppression.gif"));
	
	jpButton.add(jbCreer);
	jpButton.add(jbModif);
	jpButton.add(jbSearch);
	jpButton.add(jbSuppr);
	
	//Panel Infos générales
	jlNom = new JLabel ("Nom espece : ");
	
	jtNom = new JTextField ();
	jtNom.setPreferredSize(dimTextField);
	
	jpNom.add(jlNom);
	jpNom2.add(jtNom);
	
	jpInfosGenerales1.add(jpNom);
	jpInfosGenerales2.add(jpNom2);
	
	jpInfosGenerales.setBorder(BorderFactory.createTitledBorder(border1,"Informations générales"));
	
	jpInfosGenerales.add(jpInfosGenerales1,BorderLayout.WEST);
	jpInfosGenerales.add(jpInfosGenerales2,BorderLayout.EAST);
	
	jpPrincipal.add(jpInfosGenerales,BorderLayout.CENTER);
	jpPrincipal.add(jpBoutonsBas,BorderLayout.SOUTH);
	
	//boutons en bas du panel
	jbInserer = new JButton("Insérer");
	jbReinitialiser = new JButton("Réinitialiser");
	jbRetour = new JButton("Retour");
	
	jpBoutonsBas.add(jbInserer);
	jpBoutonsBas.add(jbReinitialiser);
	jpBoutonsBas.add(jbRetour);
	
	this.add(jpButton, BorderLayout.WEST);
	this.add(jpPrincipal, BorderLayout.CENTER);
	
	

	jbInserer.addActionListener(new MonActionListener());
	jbReinitialiser.addActionListener(new MonActionListener());
	jbRetour.addActionListener(new MonActionListener());
	jbModif.addActionListener(new MonActionListener());
	jbSearch.addActionListener(new MonActionListener());
	jbSuppr.addActionListener(new MonActionListener());
}

class MonActionListener implements ActionListener
{

	@Override
	public void actionPerformed(ActionEvent e) {

		// reinitialiser
		if(e.getSource()==jbReinitialiser)
		{
			int retour = JOptionPane.showConfirmDialog(null,
		             "Voulez vous reinitialiser ?", 
		             "retour ?",
		             JOptionPane.OK_CANCEL_OPTION);
			
			if(retour == JOptionPane.OK_OPTION){
				jtNom.setText("");
				
				jtNom.setBackground(Color.WHITE);
				jtNom.setForeground(Color.BLACK);
				
				
			}
		}
		
		// creer	
		if(e.getSource()==jbInserer)
		{
			String nom =	jtNom.getText();
			nom.replaceAll("\\'", "\\\\'" ) ;
			nom.replaceAll("\\\"", "\\\\'" ) ;
			

			boolean faux = false;
			
		
			
			if (faux || nom.isEmpty() )
			{	
				
				JOptionPane.showMessageDialog( null, "Vous n'avez pas rempli correctement tous les champs", "creation poisson", JOptionPane.INFORMATION_MESSAGE);
				
			}
			//Si des champs sont vides, ils se colorent en rouge
			else
			{
				int effectif = 0;
				PoissonDTO poissonDTO = new PoissonDTO(nom);
				
					try {
						GererPoissonCtrl.creerPoisson(poissonDTO);
					} catch (AquariumTechniqueException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (HelperException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				
				JOptionPane.showMessageDialog( null, "Le poisson a bien été ajouté !", "creation poisson", JOptionPane.INFORMATION_MESSAGE);
				
				
				//On remet a blanc le formulaire,
				
				jtNom.setText("");
				
			}
		}
		
		if(e.getSource()==jbModif){
			CardLayout cl = (CardLayout)(pc.getLayout());
        	cl.show(pc, "ModifierPoisson");
		}
		if(e.getSource()==jbSearch){
			CardLayout cl = (CardLayout)(pc.getLayout());
        	cl.show(pc, "RechercherPoisson");
		}
		if(e.getSource()==jbSuppr){
			CardLayout cl = (CardLayout)(pc.getLayout());
        	cl.show(pc, "SupprimerPoisson");
		}
		
		if(e.getSource()==jbRetour)
		{
			int retour = JOptionPane.showConfirmDialog(null,
		             "Voulez vous retourner au menu principal ?", 
		             "retour ?",
		             JOptionPane.OK_CANCEL_OPTION);
			
			if(retour == JOptionPane.OK_OPTION){
				CardLayout cl = (CardLayout)(pc.getLayout());
				cl.show(pc, "Menu");
			}				
		}
	}
}

}
	

